package com.empire.module.member.service.cases;

import com.empire.module.member.controller.admin.cases.vo.CasesPageReqVO;
import com.empire.module.member.controller.admin.cases.vo.CasesSaveReqVO;
import com.empire.module.member.dal.dataobject.cases.CasesDO;
import com.empire.module.member.dal.mysql.cases.CasesMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.util.object.BeanUtils;

import javax.annotation.Resource;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.framework.security.core.util.SecurityFrameworkUtils.getLoginUserDeptId;
import static com.empire.module.member.enums.ErrorCodeConstants.CASES_NOT_EXISTS;

/**
 * 案件管理 Service 实现类
 *
 * @author 主席大人
 */
@Service
@Validated
public class CasesServiceImpl implements CasesService {

    @Resource
    private CasesMapper casesMapper;

    @Override
    public String createCases(CasesSaveReqVO createReqVO) {
        // 插入
        CasesDO cases = BeanUtils.toBean(createReqVO, CasesDO.class);
        cases.setCreateDeptId(getLoginUserDeptId());
        casesMapper.insert(cases);
        // 返回
        return cases.getId();
    }

    @Override
    public void updateCases(CasesSaveReqVO updateReqVO) {
        // 校验存在
        validateCasesExists(updateReqVO.getId());
        // 更新
        CasesDO updateObj = BeanUtils.toBean(updateReqVO, CasesDO.class);
        casesMapper.updateById(updateObj);
    }

    @Override
    public void deleteCases(String id) {
        // 校验存在
        validateCasesExists(id);
        // 删除
        casesMapper.deleteById(id);
    }

    private void validateCasesExists(String id) {
        if (casesMapper.selectById(id) == null) {
            throw exception(CASES_NOT_EXISTS);
        }
    }

    @Override
    public CasesDO getCases(String id) {
        return casesMapper.selectById(id);
    }

    @Override
    public PageResult<CasesDO> getCasesPage(CasesPageReqVO pageReqVO) {
        return casesMapper.selectPage(pageReqVO);
    }

}